package chabernac.android.wordraster.resource;

import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import chabernac.android.wordraster.model.WordRaster;
import chabernac.android.wordraster.rastersolver.CommentWordFilter;
import chabernac.android.wordraster.rastersolver.CompositeWordFilter;
import chabernac.android.wordraster.rastersolver.DataBaseLoader;
import chabernac.android.wordraster.rastersolver.InMemoryTreeDB;
import chabernac.android.wordraster.rastersolver.InvalidWordFilter;
import chabernac.android.wordraster.rastersolver.LengthWordFilter;
import chabernac.android.wordraster.rastersolver.URLWordLoader;
import chabernac.android.wordraster.rastersolver.WordRasterFactory;
import chabernac.android.wordraster.rastersolver.WordRasterSolver;
import chabernac.android.wordraster.rastersolver.iStringDatabase;
import chabernac.android.wordraster.rastersolver.iWordFilter;
import chabernac.android.wordraster.rastersolver.iWordLoader;

import com.sun.jersey.spi.resource.Singleton;

@Path("/wordraster")
@Singleton

public class WordRasterResource {
	private static final Logger LOGGER = Logger.getLogger(WordRasterResource.class.getName());
	private DataBaseLoader myDataBaseLoader = null;
	private WordRasterSolver myWordRasterSolver  = null;
	private WordRasterFactory myWordRasterFactory = null;

	public WordRasterResource(){
		init();
	}

	private void init(){
		LOGGER.log( Level.INFO, "Initiliazing Raster factory resource" );
		iWordLoader theWordLoader = new URLWordLoader( "http://wordbattle.googlecode.com/files" );
		iWordFilter theWordFilter = new CompositeWordFilter()
		.addFilter( new CommentWordFilter() )
		.addFilter( new LengthWordFilter( 3, 10 ))
		.addFilter( new InvalidWordFilter() );
		iStringDatabase theStringDatabase = new InMemoryTreeDB();  

		myDataBaseLoader = new DataBaseLoader( theWordLoader, theWordFilter, theStringDatabase, -1 );
		myWordRasterSolver = new WordRasterSolver( theStringDatabase );
		myWordRasterFactory = new WordRasterFactory( theStringDatabase );
		LOGGER.log( Level.INFO, "Raster factory resource initilized" );
	}

	/**
	 * http://wordbattle-server.appspot.com/resources/wordraster/language/nl/grid/4x4 
	 */
	@GET
	@Path("/language/{lang}/grid/{rows}x{columns}")
	@Consumes("text/plain")
	//	@Produces(MediaType.APPLICATION_JSON)
	@Produces(MediaType.TEXT_PLAIN)
	public String createRaster(
			@PathParam("lang") String aLanguage,
			@PathParam("rows") int aRows, 
			@PathParam("columns") int aColumns){

		//first lets make sure the language is loaded
		LOGGER.log( Level.INFO, "Trying to create raster: " + aRows + " x " + aColumns + " " + aLanguage );
		myDataBaseLoader.loadLanguage( aLanguage );

		//create the raster
		WordRaster theRaster = myWordRasterFactory.createRaster( aLanguage, aRows, aColumns );

		//solve the raster and store the result in the raster
		Set<String> theWords = myWordRasterSolver.solve( theRaster, 3 );
		theRaster.setWordsInRaster( theWords );

		//		return theRaster;

		String theReply = theRaster.toString() + ";";

		for(String theWord : theWords){
			theReply += theWord;
			theReply += ";";
		}
		return theReply;
	}
}
